<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('tasks', function (Blueprint $table) {
            if (!Schema::hasColumn('tasks', 'billing_unit')) {
                $table->string('billing_unit', 20)->default('日')->after('max_fee')->comment('计费单位');
            }
        });

        if (DB::connection()->getDriverName() === 'mysql') {
            DB::statement("ALTER TABLE `tasks` MODIFY `settlement_cycle` ENUM('daily','weekly','monthly','yearly','per_task') COMMENT '结算周期'");
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('tasks', function (Blueprint $table) {
            if (Schema::hasColumn('tasks', 'billing_unit')) {
                $table->dropColumn('billing_unit');
            }
        });

        if (DB::connection()->getDriverName() === 'mysql') {
            DB::statement("ALTER TABLE `tasks` MODIFY `settlement_cycle` ENUM('daily','monthly','yearly','per_task') COMMENT '结算周期'");
        }
    }
};
